package com.xuecheng.learning.service;

import com.xuecheng.framework.domain.course.response.TeachplanMediaPub;
import com.xuecheng.framework.domain.learning.XcLearningCourse;
import com.xuecheng.framework.domain.learning.response.GetUrlResult;
import com.xuecheng.framework.domain.media.response.MediaCode;
import com.xuecheng.framework.domain.task.XcTaskHis;
import com.xuecheng.framework.exception.CustomExcetionCast;
import com.xuecheng.framework.model.response.CommonCode;
import com.xuecheng.framework.model.response.ResponseResult;
import com.xuecheng.learning.client.SearchClient;
import com.xuecheng.learning.dao.XcLearningCourseRepository;
import com.xuecheng.learning.dao.XcTaskHisRepository;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.List;

@Service
public class LearningService {
    @Autowired
    SearchClient searchClient;
    @Autowired
    XcTaskHisRepository xcTaskHisRepository;
    @Autowired
    XcLearningCourseRepository xcLearningCourseRepository;

    /**
     * @Description: 查询课程权限，url
     * @Param:
     * @return:
     * @Author: Mr.Yuan
     * @Date:
     */
    public GetUrlResult findUrl(String courseid, String teachpalnId) {
        if (StringUtils.isEmpty(courseid) || StringUtils.isEmpty(teachpalnId)) {
            throw CustomExcetionCast.getCusException(CommonCode.INVALIDPARAM);
        }
//        远程调用
        List<TeachplanMediaPub> media = searchClient.findMedia(teachpalnId);
        if (media == null) {
            throw CustomExcetionCast.getCusException(MediaCode.MERGE_URL_FAIL);
        }
        TeachplanMediaPub mediaPub = media.get(0);
        String mediaUrl = mediaPub.getMediaUrl();
        if(StringUtils.isEmpty(mediaUrl)){
            return new GetUrlResult(MediaCode.MERGE_URL_FAIL);
        }
        return new GetUrlResult(CommonCode.SUCCESS,mediaUrl);
    }
    /**
    * @Description: 添加选课
    * @Param:
    * @return:
    * @Author: Mr.Yuan
    * @Date:
    */
    @Transactional
    public ResponseResult addChooseCourse(XcLearningCourse xcLearningCourse, XcTaskHis xcTaskHis){
        String courseId = xcLearningCourse.getCourseId();
        String userId = xcLearningCourse.getUserId();
        if(StringUtils.isEmpty(courseId)||StringUtils.isEmpty(userId)){
            throw CustomExcetionCast.getCusException(CommonCode.INVALIDPARAM);
        }
        XcLearningCourse learningCourse = xcLearningCourseRepository.findXcLearningCoursesByUserIdAndCourseId(userId, courseId);
        if(learningCourse!=null){
//            更新
            BeanUtils.copyProperties(xcLearningCourse,learningCourse);
            xcLearningCourseRepository.save(learningCourse);
        }else {
//            添加
            xcLearningCourseRepository.save(xcLearningCourse);
        }
//        添加选课历史记录
        XcTaskHis taskHis = xcTaskHisRepository.save(xcTaskHis);
        System.out.println(taskHis.getRequestBody());

        return new ResponseResult(CommonCode.SUCCESS);
    }
}
